<?php
namespace app\crm\controller;
use app\common\controller\UserBase; 
use app\crm\model\CrmCustomer;
use app\crm\model\CrmContact;
use app\crm\model\CrmUser;
use app\user\model\FieldData;
use app\user\model\User;

class Customer extends UserBase
{
    public function initialize()
    {
        parent::initialize();
        //获取字段列表
        $this->field = FieldData::get_field_list('1');
        //获取客服列表
        $this->kefu = get_dict_list('5');
        //获取用户列表
        $this->user_list = User::where('status',1)->select();
    }
    //客户列表
    public function index()
    {   
        $field = FieldData::get_field_list('1');
        return $this->fetch('index',[
            'field'  => $field,
            'kefu'      => $this->kefu,
        ]);
    }
    public function index_json($limit='')
    {
        $param = $this->request->param();
        $customer = new CrmCustomer();
        //判断查看权限
        $customer = $customer->where('id','in', my_select_auth());
        if ($param['keywords'] !='') {
            $keywords = $param['keywords'];
            //$where['name'] = ['like', '%$name%'];
            $customer = $customer->where('name','like','%'.$keywords.'%');
        }
        if ($param['industry'] !='') {
            //$where['from'] = $param['from'];
            $customer = $customer->where('industry',$param['industry']);
        }
        if ($param['source'] !='' ){
            //$where['from'] = $param['from'];
            $customer = $customer->where('source',$param['source']);
        }
        if ($param['status']!='' ){
            //$where['from'] = $param['from'];
            $customer = $customer->where('status',$param['status']);
        }
        //$customer = $customer->whereOr('user_id', UserId());
        $list = $customer->with(['industry','source','level','status','create_user','crmuser'])->order('sort_order desc,id desc')->paginate($limit);
        $this->result($list);
    }
    //添加客户
    public function add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $param['user_id'] = UserId();
            $param['shop_id'] = ShopId();
            $param['jdata'] = $param;
            $result = CrmCustomer::create($param);
            if ($result == true) {
                //客服绑定
                CrmUser::update_user($param['kefu'],$result->id);
                insert_user_log('添加了客户信息');
                $this->success('添加成功');
            } else {
                $this->error($this->errorMsg);
            }
        }
        
        return $this->fetch('save', [
            'field'     => $this->field,
            'kefu'      => $this->kefu,
            'user_list' => $this->user_list
        ]);
    }
    //编辑客户
    public function edit($id)
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //更新数据
            $verify = input('_verify', true);
            //验证规则
            if($verify!='0'){
                $param['jdata'] = $param;
            }
            $result = CrmCustomer::update($param,['id'=>$param['id']]);
            if ($result == true) {
                //客服绑定
                CrmUser::update_user($param['kefu'],$param['id']);
                insert_user_log('修改了客户信息');
                $this->success('修改成功');
            } else {
                $this->error($this->errorMsg);
            }
        }
        $data = CrmCustomer::with(['crmuser'])->where('id', $id)->find();
        //$this->result($data);
        return $this->fetch('save', [
            'data'      => $data,
            'field'     => $this->field,
            'kefu'      => $this->kefu,
            'user_list' => $this->user_list
        ]);
    }
    //删除客户
    public function del()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            CrmCustomer::destroy($param['id']);
            insert_user_log('删除了客户');
            $this->success('删除成功');
        }
    }
    //
    public function detail($id)
    {   
        $customer = new CrmCustomer();
        //获取数据
        $data = $customer->with(['crmuser'])->where('id',$id)->find();
        
   //     $this->result($data);
        return $this->fetch('detail',[
            'data'  => $data,
            'field' => $this->field,
            'kefu'  => $this->kefu,
        ]);
    }
    
    
    
}
